package com.irdstudio.rsp.rule.service.impl;

import java.util.List;
import java.util.Objects;

import com.irdstudio.rsp.drools.beans.RuleParamBean;
import com.irdstudio.rsp.rule.common.BizUtil;
import com.irdstudio.rsp.rule.common.constant.Constant;
import com.irdstudio.rsp.rule.dao.SRuleCollInfoDao;
import com.irdstudio.rsp.rule.service.vo.RuleParamVO;
import com.irdstudio.sdk.ssm.vo.UserInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.rsp.rule.service.facade.SRuleCollParamService;
import com.irdstudio.rsp.rule.dao.SRuleCollParamDao;
import com.irdstudio.rsp.rule.dao.domain.SRuleCollParam;
import com.irdstudio.rsp.rule.service.vo.SRuleCollParamVO;
import com.irdstudio.sdk.beans.core.base.FrameworkService;
/**
 * <p>ServiceImpl: 规则集参数信息				<p>
 * @author AutoCoder
 * @date 2019-10-30
 */
@Service("sRuleCollParamServiceImpl")
public class SRuleCollParamServiceImpl implements SRuleCollParamService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SRuleCollParamServiceImpl.class);

	@Autowired
	private SRuleCollParamDao sRuleCollParamDao;

	@Autowired
	private SRuleCollInfoDao sRuleCollInfoDao;
	
	/**
	 * 新增操作
	 */
	public int insertSRuleCollParam(SRuleCollParamVO inSRuleCollParamVo) {
		logger.debug("当前新增数据为:"+ inSRuleCollParamVo.toString());
		int num = 0;
		try {
			SRuleCollParam sRuleCollParam = new SRuleCollParam();
			beanCopy(inSRuleCollParamVo, sRuleCollParam);
			num = sRuleCollParamDao.insertSRuleCollParam(sRuleCollParam);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SRuleCollParamVO inSRuleCollParamVo) {
		logger.debug("当前删除数据条件为:"+ inSRuleCollParamVo);
		int num = 0;
		try {
			SRuleCollParam sRuleCollParam = new SRuleCollParam();
			beanCopy(inSRuleCollParamVo, sRuleCollParam);
			num = sRuleCollParamDao.deleteByPk(sRuleCollParam);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ inSRuleCollParamVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SRuleCollParamVO inSRuleCollParamVo) {
		logger.debug("当前修改数据为:"+ inSRuleCollParamVo.toString());
		int num = 0;
		try {
			SRuleCollParam sRuleCollParam = new SRuleCollParam();
			beanCopy(inSRuleCollParamVo, sRuleCollParam);
			num = sRuleCollParamDao.updateByPk(sRuleCollParam);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
			throw e;
		}
		logger.debug("根据条件:"+ inSRuleCollParamVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public SRuleCollParamVO queryByPk(SRuleCollParamVO inSRuleCollParamVo) {
		
		logger.debug("当前查询参数信息为:"+ inSRuleCollParamVo);
		try {
			SRuleCollParam querySRuleCollParam = new SRuleCollParam();
			beanCopy(inSRuleCollParamVo, querySRuleCollParam);
			SRuleCollParam queryRslSRuleCollParam = sRuleCollParamDao.queryByPk(querySRuleCollParam);
			if (Objects.nonNull(queryRslSRuleCollParam)) {
				SRuleCollParamVO outSRuleCollParamVo = beanCopy(queryRslSRuleCollParam, new SRuleCollParamVO());
				logger.debug("当前查询结果为:"+ outSRuleCollParamVo.toString());
				return outSRuleCollParamVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 保存页面参数配置
	 * @param voList
	 * @return
	 */
	@Override
	public int updateSRuleCollParams(List<RuleParamVO> voList, UserInfo userInfo) {
		int num = 0;
		if (CollectionUtils.isEmpty(voList)) {
			return 0;
		}
		for (RuleParamVO vo : voList) {
			if (StringUtils.isBlank(vo.getParamId())) {
				// 为空不需要更新
				continue;
			}
			try {
				SRuleCollParam paramVO = new SRuleCollParam();
				paramVO.setParamId(vo.getParamId());
				paramVO.setDtlId(vo.getDtlId());
				paramVO = sRuleCollParamDao.queryByPk(paramVO);
				if (paramVO != null) {
					// 需要更新
					paramVO.setParamVal(vo.getParamVal());
					paramVO.setUpdateTime(BizUtil.getCurrentTime());
					paramVO.setUpdateUser(userInfo.getUserId());
					num += sRuleCollParamDao.updateByPk(paramVO);
				} else {
					// 需要插入
					paramVO = new SRuleCollParam();
					beanCopy(vo, paramVO);
					paramVO.setCreateTime(BizUtil.getCurrentTime());
					paramVO.setCreateUser(userInfo.getUserId());
					paramVO.setUpdateTime(BizUtil.getCurrentTime());
					paramVO.setUpdateUser(userInfo.getUserId());
					num += sRuleCollParamDao.insertSRuleCollParam(paramVO);
				}
			} catch (Exception e) {
				logger.error("修改数据发生异常!", e);
				num = -1;
				throw e;
			}
		}

		return num;
	}


	@Override
	public List<RuleParamVO> queryAllWithRuleParam(SRuleCollParamVO sRuleCollParamVo) {
		List<RuleParamVO> list = null;
		try {
			List<RuleParamBean> params = sRuleCollInfoDao.querySRuleCollParamByDtlId(sRuleCollParamVo);
			logger.debug("当前查询规则集明细参数配置数量为:"+ params.size());
			list = (List<RuleParamVO>) beansCopy(params, RuleParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollParamVO> queryAllOwner(SRuleCollParamVO sRuleCollParamVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<SRuleCollParamVO> list = null;
		try {
			List<SRuleCollParam> sRuleCollParams = sRuleCollParamDao.queryAllOwnerByPage(sRuleCollParamVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sRuleCollParams.size());
			pageSet(sRuleCollParams, sRuleCollParamVo);
			list = (List<SRuleCollParamVO>) beansCopy(sRuleCollParams, SRuleCollParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollParamVO> queryAllCurrOrg(SRuleCollParamVO sRuleCollParamVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<SRuleCollParam> sRuleCollParams = sRuleCollParamDao.queryAllCurrOrgByPage(sRuleCollParamVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+sRuleCollParams.size());
		List<SRuleCollParamVO> list = null;
		try {
			pageSet(sRuleCollParams, sRuleCollParamVo);
			list = (List<SRuleCollParamVO>) beansCopy(sRuleCollParams, SRuleCollParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SRuleCollParamVO> queryAllCurrDownOrg(SRuleCollParamVO sRuleCollParamVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<SRuleCollParam> sRuleCollParams = sRuleCollParamDao.queryAllCurrDownOrgByPage(sRuleCollParamVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ sRuleCollParams.size());
		List<SRuleCollParamVO> list = null;
		try {
			pageSet(sRuleCollParams, sRuleCollParamVo);
			list = (List<SRuleCollParamVO>) beansCopy(sRuleCollParams, SRuleCollParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
